// repair_pkg/pages/form/index.js
import wxValidate from 'wechat-validate'

Page({

  /**
   * 页面的初始数据
   */
  behaviors: [wxValidate],
  data: {
    houseViewVisible: false,
    houseList: [],
    houseId:'',
    houseInfo:'',

    repairViewVisible: false,
    repairItem:[],
    repairItemId:'',
    repairItemName:"",

    dateViewVisible:false,
    currentDate:Date.now(),
    appointment:'',

    mobile: '',
    description: '',
    attachment: [],
  },

  rules: {
    houseId: [{ required: true, message: '请选择报修房屋!' }],
    repairItemId: [{ required: true, message: '请选择维修的项目!' }],
    mobile: [
      { required: true, message: '请填写手机号码!' },
      { pattern: /^1[3-8]\d{9}$/, message: '请填写正确的手机号码!' },
    ],
    appointment: [{ required: true, message: '请选择预约日期!' }],
    description: [{ required: true, message: '请填写问题描述!' }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(id) {
    this.loadHouseListData()
    this.loadRepairItemData()
     // 如果有id表明是修改操作
     if (id) this.getRepairDetail(id.id)
  },

  async getRepairDetail(id) {
    // 调用接口
    const { code, data: repairDetail } = await wx.http.get('/repair/' + id)
    // 检测是否调用成功
    if (code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({ ...repairDetail })
  },

  async loadHouseListData() {
    const {code,data:houseList} = await wx.http.get('/house')
    if (code !== 10000) {
      return wx.utils.toast()
    }
    this.setData({houseList})
  },

  async loadRepairItemData() {
    const {code,data:repairItem} = await wx.http.get('/repairItem')
    if (code !== 10000) {
      return wx.utils.toast()
    }
    this.setData({repairItem})
  },

  openHouseView() {
    this.setData({houseViewVisible:true})
  },

  closeHouseView() {
    this.setData({houseViewVisible:false})
  },

  selectHouseInfo(ev) {
    this.setData({
      houseId:ev.detail.id,
      houseInfo:ev.detail.name
    })
  },

  openRepairView() {
    this.setData({repairViewVisible:true})
  },

  closeRepairView() {
    this.setData({repairViewVisible:false})
  },

  selectRepairInfo(ev) {
    this.setData({
      repairItemId:ev.detail.id,
      repairItemName:ev.detail.name
    })
  },

  openDateView() {
    this.setData({dateViewVisible:true})
  },

  closeDateView() {
    this.setData({dateViewVisible:false})
  },

  selectDate(ev) {
    this.setData({
      appointment:wx.utils.dateFormat(ev.detail),
      dateViewVisible:false
    })
  },

  uploadPicture(ev) {
    const {file} = ev.detail
    console.log(file.url)
    wx.uploadFile({
      url:wx.http.baseURL + "/upload",
      filePath: file.url,
      name:'file',
      header: {
        Authorization:'Bearer ' + getApp().token,
      },
      success:(result) => {
        const data = JSON.parse(result.data)
        if (data.code !== 10000) {
          return wx.utils.toast('文件上传失败！')
        }
        const {attachment} = this.data
        attachment.push(data.data)
        this.setData({attachment})
      }
    })
  },

  async submitForm() {
    if (!this.validate()) {
      return
    }
    const {id,houseId,repairItemId,mobile,appointment,description,attachment} = this.data
    const {code} = await wx.http.post('/repair', {
      id,
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment
    })
    if (code !== 10000) {
      return wx.utils.toast('在线报修失败！')
    }
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index'
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})